package com.heatup.apt.common.dao;

import com.heatup.apt.model.CashcardDetail;
import com.heatup.apt.model.CashcardDetailApiResultModel;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.mapper.AutoMapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.*;

/**
 *
 * CashcardDetail 表数据库控制层接口
 *
 */
@Component
public interface CashcardDetailMapper extends AutoMapper<CashcardDetail> {

	CashcardDetail selectByChangeCode(@Param("code") String code);

	@Select({ "select",
			"id, cashcard_id AS cashcardId, cashcard_code AS cashcardCode, make_time AS makeTime, user_id AS userId, is_used AS isUsed, use_time AS useTime, is_valid AS isValid",
			"from cashcard_detail", "where binary cashcard_code = #{code,jdbcType=VARCHAR}",
			"and user_id = #{userId,jdbcType=INTEGER}", "order by id desc limit 1" })
	CashcardDetail selectByCodeAndUserID(@Param("code") String code, @Param("userId") Integer userId);

	/**
	 * 作废未领取的储值卡券
	 *
	 * @param id
	 *            储值卡券ID
	 * @return
	 */
	int updateCashcardToOfflineByCashcardId(Integer id);
	
	/**
	 * 分页查询
	 * @param page
	 * @param map
	 * @return
	 */
	List<CashcardDetail> quertCashcardDetailPage(Page<CashcardDetail> page,Map<String,Object> map);
	
	/**
	 * 查询某个储值卡下面有效的储值卡可兑换的还有多少
	 * @param map
	 * @return
	 */
	int queryCashcardDetailCount(Map<String,Object> map);
	
	/**
	 * 批量插入储值卡明细
	 * @param list
	 * @return
	 */
	int insertCashCardDetailBatch(List<CashcardDetail> list);
	
	/**
	 * 根据储值卡批次查询批次下的所有有效 未使用的 储值卡明细
	 * @param cashcardId
	 * @return
	 */
	List<CashcardDetailApiResultModel> queryCashcardDetailModel(@Param("cashcardId") Integer cashcardId);
	
	/**
	 * 查询该用户在同一个批次兑换过了几张储值卡兑换码
	 * @param cashcardId
	 * @param userId
	 * @return
	 */
	int queryCountByUserIdAndCashcardId(@Param("cashcardId") Integer cashcardId,@Param("userId") Integer userId);

	/**
	 * 根据储值卡批次充值
	 */
	@Select({
		"SELECT ci.cashcard_code as cashcardCode FROM cashcard_detail ci WHERE ci.cashcard_id=#{cardId} AND ci.is_used=0 AND ci.is_valid=1 LIMIT 1"
	})
	CashcardDetail selectCardNoByid(@Param("cardId") Integer cardId);
}